package models

import (
	"crawl_toutiao/models/toutiao"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"net/url"
)

func init() {
	orm.Debug = true

	orm.RegisterModel(new(toutiao.Keyword))
	orm.RegisterModel(new(toutiao.UrlInfo))
	orm.RegisterModel(new(toutiao.Picture))
	orm.RegisterModel(new(toutiao.Url))

	orm.RegisterModel(new(toutiao.DailyNum))

	orm.RegisterDriver("mysql", orm.DRMySQL)
	c := beego.AppConfig

	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&loc=%s",
		c.String("db_user"),
		c.String("db_pass"),
		c.String("db_host"),
		c.DefaultString("db_port", "3306"),
		c.String("db_name"), url.QueryEscape("Asia/Shanghai"),
	)

	orm.RegisterDataBase("default", "mysql", dsn)

	orm.RunSyncdb("default", false, true)
}
